import { createWebHashHistory, createRouter } from 'vue-router'

/* Layout */
import Layout from '../views/Layout/Layout.vue'
import LayoutMobile from '@/mobile/Layout/Layout.vue'
import store from '../store';

const routes = [
  {
    path: '/',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/index',
        name: 'index',
        meta: { title: '首页' },
        component: () => import('../views/Index.vue')
      },
      {
        path: '/torrent',
        name: 'torrent',
        meta: { title: '种子管理' },
        component: () => import('../views/Torrent/Torrent.vue')
      },
      {
        path: '/rss',
        name: 'rss',
        meta: { title: 'RSS' },
        component: () => import('../views/Torrent/Rss.vue')
      },
      {
        path: '/client',
        name: 'client',
        meta: { title: '下载器' },
        component: () => import('../views/Client/Client.vue')
      },
      {
        path: '/btsite',
        name: 'btsite',
        meta: { title: '站点配置' },
        component: () => import('../views/BTSite/BTSite.vue')
      },
      {
        path: '/category',
        name: 'category',
        meta: { title: '分类配置' },
        component: () => import('../views/Category/Category.vue')
      },
      {
        path: '/library',
        name: 'library',
        meta: { title: '媒体库' },
        component: () => import('../views/Library/Library.vue')
      },
      {
        path: '/config',
        name: 'config',
        meta: { title: '基础配置' },
        component: () => import('../views/Config/Config.vue')
      },
      {
        path: '/about',
        name: 'about',
        meta: { title: '关于' },
        component: () => import('../views/About.vue')
      }
    ]
  },
  // 移动端
  {
    path: '/m',
    component: LayoutMobile,
    children: [
      {
        path: '/m',
        name: 'home-mobile',
        meta: { title: '首页', keepAlive: true },
        component: () => import('@/mobile/Home/Home.vue')
      },
      {
        path: '/m/sub',
        name: 'sub-mobile',
        meta: { title: '订阅', keepAlive: true },
        component: () => import('@/mobile/Sub/Sub.vue')
      },
      {
        path: '/m/functions',
        name: 'functions-mobile',
        meta: { title: '功能', keepAlive: true },
        component: () => import('@/mobile/Functions/Functions.vue')
      },
      {
        path: '/m/config',
        name: 'config-mobile',
        meta: { title: '设置' },
        component: () => import('@/mobile/Config/Config.vue')
      },
      // 二级页面
      {
        path: '/m/sub/detail',
        name: 'sub-detail-mobile',
        meta: { title: '订阅详情', keepAlive: false, navBar: true },
        component: () => import('@/mobile/Sub/Detail.vue')
      },
      {
        path: '/m/torrent',
        name: 'torrent-mobile',
        meta: { title: '种子管理', keepAlive: true, navBar: true },
        component: () => import('@/mobile/Torrent/Torrent.vue')
      },
      {
        path: '/m/rss',
        name: 'rss-mobile',
        meta: { title: 'RSS', keepAlive: true, navBar: true },
        component: () => import('@/mobile/Torrent/Rss.vue')
      },
      {
        path: '/m/torrent/detail',
        name: 'torrent-detail-mobile',
        meta: { title: '种子详情', keepAlive: false, navBar: true },
        component: () => import('@/mobile/Torrent/Detail.vue')
      },
      {
        path: '/m/jackett',
        name: 'jackett-mobile',
        meta: { title: 'Jackett', keepAlive: true, navBar: true },
        component: () => import('@/mobile/Search/Jackett.vue')
      },
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((guard) => {
  // console.info('router.beforeEach:', guard)
  if (guard.path != '/' && !store.state.authed) {
    router.replace({
      path: "/"
    });
  } else {
    // 移动端页面
    // if (store.state.isMobile) {
    //   if (guard.path === '/torrent') {
    //     router.replace({
    //       path: "/torrent/mobile"
    //     });
    //   }
    // }
  }
});
router.onError((handler) => {
  console.log("error:", handler);
});

export default router
